package org.example.mone.task;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import org.example.constant.CommonConstant;
import org.example.enums.EDeployTaskStep;
import org.example.enums.EJobType;
import org.example.model.*;
import org.example.task.HttpTask;
import org.example.task.IExecutable;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.io.File;


public class DeployJob implements IExecutable {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeployJob.class);

    private RestTemplate restTemplate;

    private EDeployTaskStep taskStep;
    private Long jobId;

    public DeployJob(EDeployTaskStep taskStep,Long jobId) {
        this.taskStep = taskStep;
        this.jobId = jobId;
        initBean();
    }

    public DeployJob() {
        initBean();
    }



    private void initBean() {
        restTemplate = SpringUtil.getBean(RestTemplate.class);
    }

    @Override
    public Result<String> execute() {
        LOGGER.info("DeployJob start taskStep={}", taskStep);
        HttpTask httpTask = null;
        Result<String> result = null;
        switch (taskStep) {
            case CHECKHEALTH:
                LOGGER.info("DeployJob CHECKHEALTH");
                CheckHealthParam checkHealthParam = new CheckHealthParam();
                checkHealthParam.setJobId(this.jobId);
                checkHealthParam.setTaskId(1L);
                checkHealthParam.setJobType(EJobType.DEPLOY.getJobType());
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/checkhealth", JSONObject.toJSONString(checkHealthParam));
                result = httpTask.execute();
                break;
            case DISKCHECK:
                LOGGER.info("DeployJob DISKCHECK");
                DiskCheckParam diskCheckParam =  new DiskCheckParam();
                diskCheckParam.setJobId(this.jobId);
                diskCheckParam.setTaskId(2L);
                diskCheckParam.setDiskPath("D:/");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/diskcheck", JSONObject.toJSONString(diskCheckParam));
                result = httpTask.execute();
                break;
            case COPY:
                LOGGER.info("DeployJob copy");
                CopyFileParam copyFileParam = new CopyFileParam();
                String filePath = "D:/soft/FileZilla_3.62.2_win64.zip";
                copyFileParam.setJobId(this.jobId);
                copyFileParam.setTaskId(3L);
                copyFileParam.setFilePath("filePath");
                copyFileParam.setFileSize(new File(filePath).length());
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/filecopy", JSONObject.toJSONString(copyFileParam));
                result = httpTask.execute();
                break;
            case UNZIP:
                LOGGER.info("DeployJob unzip");
                UnzipFileParam unzipFileParam = new UnzipFileParam();
                unzipFileParam.setJobId(this.jobId);
                unzipFileParam.setTaskId(4L);
                unzipFileParam.setFilePath("D:/soft/FileZilla_3.62.2_win64.zip");
                unzipFileParam.setTargetPath("D:/temp/install");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/fileunzip", JSONObject.toJSONString(unzipFileParam));
                result = httpTask.execute();
                break;
            case FILECONFIG:
                LOGGER.info("DeployJob fileConfig");
                FileConfigParam fileConfigParam = new FileConfigParam();
                fileConfigParam.setJobId(this.jobId);
                fileConfigParam.setTaskId(5L);
                fileConfigParam.setFilePath("xxx");
                fileConfigParam.setTargetPath("aaa");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/fileconfig", JSONObject.toJSONString(fileConfigParam));
                result = httpTask.execute();
                break;
            case ENVCONFIG:
                LOGGER.info("DeployJob envConfig");
                EnvConfigParam envConfigParam = new EnvConfigParam();
                envConfigParam.setJobId(this.jobId);
                envConfigParam.setTaskId(6L);
                envConfigParam.setScriptPath("install path");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/envconfig", JSONObject.toJSONString(envConfigParam));
                result = httpTask.execute();
                break;
            case END:
                LOGGER.info("DeployJob end .");
                break;
            default:
                LOGGER.error("DeployJob error DeployTaskStep ...taskStep={}",taskStep);
        }

        LOGGER.info("更新JOB任务状态。。。。。。。toDB,{}", result);
        return result;
    }
}
